import requests
import hmac
import hashlib
import time


def get_open_orders_simple(api_key, api_secret, symbol=None, testnet=True):
    """简化版：查询未成交订单"""
    base_url = "https://testnet.binance.vision" if testnet else "https://api.binance.com"
    endpoint = "/api/v3/openOrders"

    # 获取服务器时间
    timestamp = int(time.time() * 1000)
    try:
        time_resp = requests.get(f"{base_url}/api/v3/time")
        timestamp = time_resp.json()['serverTime']
    except:
        pass

    # 构建查询参数
    params = f"timestamp={timestamp}&recvWindow=5000"
    if symbol:
        params = f"symbol={symbol}&{params}"

    # 创建签名
    signature = hmac.new(
        api_secret.encode('utf-8'),
        params.encode('utf-8'),
        hashlib.sha256
    ).hexdigest()

    # 发送请求
    url = f"{base_url}{endpoint}?{params}&signature={signature}"
    headers = {"X-MBX-APIKEY": api_key}

    response = requests.get(url, headers=headers)
    return response.json()


# 使用示例
# api_key = "your_api_key"
# api_secret = "your_api_secret"
api_key = "oMlYVBgANplqH7XilK0rzJ47r7p1R3MWoywVNBYOGvj5UHYXebQnxZjX9kLpdd5V"
api_secret = "8587vRjV45QsPPXhlrn3vcBdUwQJFA6HIBqfaVbJO7UqhhqTcVrQCR4GEO6w4OtN"
# 查询BNBUSDT的未成交订单
orders = get_open_orders_simple(api_key, api_secret, "BNBUSDT", testnet=False)
print("未成交订单:", orders)